package main

import (
	"fmt"
	"go_web_todo/cache"
	"go_web_todo/dao"
	"go_web_todo/models"
	"go_web_todo/routers"
	"go_web_todo/setting"
)

// @title ToDoList API
// @version 0.0.1
// @description This is a sample Server pets
// @name FanOne
// @BasePath /api/v1
func main() {
	// 加载配置文件(需要在命令行传递参数告知配置文件的位置，比如 go run main.go conf/config.ini)
	if err := setting.Init("conf/config.ini"); err != nil {
		fmt.Printf("load config from file failed, err:%v\n", err)
		return
	}
	err := dao.InitMysql(setting.Conf.MySQLConfig)
	if err != nil {
		fmt.Printf("init mysql connect failed,err:%v\n", err)
		return
	}
	fmt.Println("MySQL数据库连接成功！")
	defer dao.Close() //程序退出关闭MySQL数据库连接
	_, err = cache.InitRedis(setting.Conf.RedisConfig)
	if err != nil {
		fmt.Printf("init redis connect failed,err:%v\n", err)
		return
	}
	fmt.Println("Redis连接成功！")
	//自动迁移数据
	dao.DB.Set("gorm:table_options", "charset=utf8mb4").AutoMigrate(&models.User{}, &models.Task{})
	//设置路由组
	r := routers.SetupRouter()
	if err := r.Run(setting.Conf.HttpPort); err != nil {
		fmt.Printf("server startup failed, err:%v\n", err)
	}
}
